package widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

public class FlickFrameLayout extends FrameLayout {
	Context mContext = null;
	boolean isFlick = false;
	float firstX;
	float firstY;
	float lastX;
	float lastY;

	OnRightFilckListener mOnRightFilckListener;
	OnLeftFilckListener mOnLeftFilckListener;

	public FlickFrameLayout(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
		mContext = context;
	}

	public FlickFrameLayout(Context context, AttributeSet attr) {
		super(context, attr);
		mContext = context;
		init();
	}

	void init() {

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
//			if (!isFlick) {
				isFlick = true;
				firstX = ev.getX();
				firstY = ev.getY();
//			}
//			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			if (isFlick) {
				isFlick = false;
				lastX = ev.getX();
				lastY = ev.getY();

				boolean isOverFlickLength = Math.abs(lastX - firstX) > 70;
				boolean isRightFlick = lastX - firstX > 0;

				if (isOverFlickLength) {
					if (isRightFlick) {
						mOnRightFilckListener.onRightFilck(this);
					} else {
						mOnLeftFilckListener.onLeftFilck(this);
					}
					return true;
				}
			}
			break;
		}
		return super.dispatchTouchEvent(ev);
	}

	public interface OnRightFilckListener {
		public void onRightFilck(View v);
	}

	public OnRightFilckListener getOnRightFilckListener() {
		return mOnRightFilckListener;
	}

	public void setOnRightFilckListener(OnRightFilckListener mOnRightFilckListener) {
		this.mOnRightFilckListener = mOnRightFilckListener;
	}

	public interface OnLeftFilckListener {
		public void onLeftFilck(View v);
	}

	public OnLeftFilckListener getOnLeftFilckListener() {
		return mOnLeftFilckListener;
	}

	public void setOnLeftFilckListener(OnLeftFilckListener mOnLeftFilckListener) {
		this.mOnLeftFilckListener = mOnLeftFilckListener;
	}
}